﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Common;

namespace CIT.WMS.Msd
{
    public partial class MSD_Out_Drying : Form
    {
        DataTable dtMsd = new DataTable();

        public MSD_Out_Drying()
        {
            InitializeComponent();
        }

        private void txtSerialNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string msg = string.Empty;
                if (!ValidateInput(out msg))
                {
                    Common.PublicUtils.ShowMsg(this.lab_msg, msg, false);
                    txtSerialNumber.SelectAll();
                    txtSerialNumber.Focus();
                    return;
                }
                PublicUtils.WriteLog(txtSerialNumber.Text.Trim(), "出干燥箱", ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]), ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["QTY"], 0));
                PublicUtils.ShowMsg(this.lab_msg, msg, true);
                txtSerialNumber.SelectAll();
                txtSerialNumber.Focus();
            }
        }

        private bool ValidateInput(out string msg)
        {
            if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
            {
                msg = "ReelID不能为空";
                return false;
            }
            dtMsd = MSDService.Query(string.Format(" AND a.SerialNumber='{0}'", txtSerialNumber.Text.Trim()));
            if (dtMsd.Rows.Count == 0)//校验库存表是否存在该条码
            {
                msg = "ReelID错误";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["IsMSD"]) != "0")//校验是否是MSD
            {
                msg = "ReelID非MSD";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) != "3")//1.不是干燥状态的物料不允许通过
            {
                switch (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]))
                {
                    case "1": msg = "ReelID拆封状态，无法出干燥箱"; break;
                    case "2": msg = "ReelID密封状态，无法出干燥箱"; break;
                    case "4": msg = "ReelID出干燥箱状态，无法重复出干燥箱"; break;
                    case "5": msg = "ReelID烘烤状态，无法出干燥箱"; break;
                    case "6": msg = "ReelID结束烘烤状态，无法出干燥箱"; break;
                    case "7": msg = "ReelID暴露超时状态，无法出干燥箱"; break;
                    default: msg = "ReelID未进入干燥箱，无法出干燥箱"; break;
                }
                return false;
            }
            DataTable dt_IsOk = MSDService.Out_Drying(txtSerialNumber.Text.Trim());
            if (dt_IsOk.Rows[0][0].ToString() == "0")
            {
                msg = dt_IsOk.Rows[0][1].ToString();
                return false;
            }
            msg = "出干燥箱成功";
            return true;
        }
    }
}